package com.tanhua.sso.controller;

import com.tanhua.sso.vo.ErrorResult;
import com.tanhua.sso.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/loginReginfo")
    public ResponseEntity<Object> saveUserInfo(@RequestBody Map<String, String> data,
                                               @RequestHeader("Authorization") String token) {
        try {
            Boolean flag = userInfoService.saveUserInfo(data, token);
            if (flag == true) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String msg = "保存用户基本信息失败~";
        ErrorResult errorResult = ErrorResult.builder().errCode("000011").errMessage(msg).build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    @PostMapping("/loginReginfo/head")
    public ResponseEntity<Object> saveUserImage(@RequestParam("headPhoto") MultipartFile multipartFile,
                                                @RequestHeader("Authorization") String token) {
        try {
            boolean flag = userInfoService.saveUserImage(multipartFile, token);
            if (flag == true) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String msg = "保存用户头像图片失败~";
        ErrorResult errorResult = ErrorResult.builder().errCode("000012").errMessage(msg).build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

}
